package edu.hunau.cxb19.oop.class24;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.Test;

/**
 *
 *@author hb
 *
 * 2020-03-27 15:11:13
 */
public class FindAndMatchTest {

	@Test
	public void anyMatchTest() {
		 List<Integer> list = Arrays.asList(1,2,23,45,21,4,23);
		 //当前集合中是否有偶数
		 boolean hasEven = list.stream().anyMatch(s->{
			 System.out.println("---anyMatch--");
			 return s%2==0;});
		 System.out.println(hasEven);
	}
	
	@Test
	public void allMatchTest() {
		 List<Integer> list = Arrays.asList(1,3,23,45,21,4,23);
		 //当前集合中是否都是奇数
		 boolean hasEven = list.stream().allMatch(s->{
			 System.out.println("---allMatch--");
			 return s%2!=0;});
		 System.out.println(hasEven);
	}
	
	@Test
	public void noneMatchTest() {
		 List<Integer> list = Arrays.asList(1,3,23,45,21,4,23);
		 //当前集合中是否都是奇数
		 boolean hasEven = list.stream().noneMatch(s-> s%2==0);
		 System.out.println(hasEven);
	}
	
	@Test
	public void findFirstTest() {
		 List<Integer> list = Arrays.asList(1,2,7,4,5);
		 //获得第一个平方能够整除3的数据
		 Optional<Integer> optional = list.stream()
										     .filter(i-> i*i%3==0)
										     .findFirst();
		 //如果存在该数据，输出
		 optional.ifPresent(System.out::println);
	}
	
	@Test
	public void findAnyTest() {
		 List<Integer> list = Arrays.asList(1,2,3,4,9);
		 //获得第一个平方能够整除3的数据
		 Optional<Integer> optional = list.stream()
										     .filter(i-> i*i%3==0)
										     .findAny();
		 //如果存在该数据，输出
		 optional.ifPresent(System.out::println);
	}
}
